package com.zjitc.service.imp;

import com.zjitc.Utils.JsonUtil;
import com.zjitc.common.StaticString;
import com.zjitc.dao.UserDao;
import com.zjitc.model.User;
import com.zjitc.service.UserService;

import java.lang.reflect.Method;
import java.util.*;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

/**
 * Created by Aspire on 2018/3/28.
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private Jedis jedis;

  @Autowired
  private UserDao userDao;

  @Override
  public int batchInsert(List userList) {
    int count=userDao.batchInsert(userList);
    return count;
  }

  @Override
  public List list(Map<String, Boolean> resultMap) {
    List userList;
    List<String> resultList = getResultList(resultMap);


    userList = getPojo(resultList.toArray(new String[0]), resultMap);
    //如果为空从数据库取
    if (CollectionUtils.isEmpty(userList)) {
      userList = userDao.list(resultMap.get(StaticString.SET_AGE_MORE_THAN_17) ? 17 : null,
              resultMap.get(StaticString.SET_AGE_LESS_THAN_25) ? 25 : null,
              resultMap.get(StaticString.SET_SEX_MEN) ? "m" : "n");

      setRedis(userList, resultMap);
      userList = getPojo(resultList.toArray(new String[0]), resultMap);
    }
    return userList;
  }

  /**
   * 获取符合要求的key集合
   * @param resultMap
   * @return
   */
  private List getResultList(Map resultMap){
    List<String> resultList = new ArrayList();
    Set set = resultMap.entrySet();
    Iterator<Map.Entry<String, Boolean>> iterator = set.iterator();
    while (iterator.hasNext()) {
      Map.Entry<String, Boolean> entry = iterator.next();
      if (entry.getValue()) {
        resultList.add(entry.getKey());
      }
    }

    return resultList;
  }

  /**
   * 存储redis没有的内容
   * @param userList
   * @param resultMap
   */
  private void setRedis(List<User> userList, Map<String, Boolean> resultMap) {
    List<String> resultList = getResultList(resultMap);
    for (int i = 0; i < userList.size(); i++) {
      User user = userList.get(i);
      for (String key : resultList) {
        if (resultMap.get(key)) {
          jedis.sadd(key, String.valueOf(user.getId()));
        }
      }
      jedis.hset(StaticString.HASH_USER, user.getId().toString(), JsonUtil.objToString(user));
    }
  }

  /**
   * 从redis获取符合要求的集合
   * @param params
   * @return
   */
  private List getPojo(String[] params, Map<String, Boolean> resultMap){
    boolean isAgeSinter = resultMap.get(StaticString.SET_AGE_MORE_THAN_17) && resultMap.get(StaticString.SET_AGE_LESS_THAN_25);
    boolean isSexSinter = resultMap.get(StaticString.SET_SEX_WOMEN) && resultMap.get(StaticString.SET_SEX_MEN);
    boolean isSinter = isAgeSinter || isSexSinter;

    List userList = new ArrayList();
    Class clazz = Jedis.class;
    Method[] methods;
    Set<String> sinter = new HashSet<>();
    try {
      methods = clazz.getMethods();
      for (Method method : methods) {
        if ("sinter".equals(method.getName()) && isSinter) {
          sinter = (Set<String>) method.invoke(jedis, new Object[]{params});
          if (CollectionUtils.isEmpty(sinter)) {
            return null;
          }
          for (String key : sinter) {
            String hkey = jedis.hget(StaticString.HASH_USER, key);
            User user = JsonUtil.jsonToObj(hkey, User.class);
            userList.add(user);
          }
        }
      }
      for (Method method : methods) {
        if ("sdiff".equals(method.getName())) {
          Set<String> set1 = (Set<String>) method.invoke(jedis, new Object[]{new String[]{StaticString.SET_AGE_MORE_THAN_17, StaticString.SET_AGE_LESS_THAN_25}});
          Set<String> set2 = (Set<String>) method.invoke(jedis, new Object[]{new String[]{StaticString.SET_AGE_LESS_THAN_25, StaticString.SET_AGE_MORE_THAN_17}});
          sinter.addAll(resultMap.get(StaticString.SET_AGE_MORE_THAN_17) && !isAgeSinter ? set1 : set2);
          for (String key : sinter) {
            String hkey = jedis.hget(StaticString.HASH_USER, key);
            User user = JsonUtil.jsonToObj(hkey, User.class);
            userList.add(user);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return userList;
  }
}

